//
//  LSZInformTheDetailsViewController.m
//  PartyClassCloud
//
//  Created by 梁素珍 on 2018/12/2.
//  Copyright © 2018年 九酷. All rights reserved.
//

#import "LSZInformTheDetailsViewController.h"

@interface LSZInformTheDetailsViewController ()<UITextViewDelegate,UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UILabel *labBigTitle;
@property (weak, nonatomic) IBOutlet UILabel *labTitle;
@property (weak, nonatomic) IBOutlet UILabel *labAddress;
@property (weak, nonatomic) IBOutlet UILabel *labTime;
@property (weak, nonatomic) IBOutlet UILabel *labEndTime;
@property (weak, nonatomic) IBOutlet UILabel *labContent;
@property (weak, nonatomic) IBOutlet UIView *lowView;
@property (weak, nonatomic) IBOutlet UITextView *textViewContent;
@property (weak, nonatomic) IBOutlet UIButton *butSend;
@property(strong,nonatomic) UIBarButtonItem * rightBarItem;
@property (weak, nonatomic) IBOutlet UIButton *but_fankui;


@property(strong,nonatomic)UITableView * tableViewDetails;
@end

@implementation LSZInformTheDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"通知详情";

    self.labBigTitle.hidden = YES;
    self.labTitle.hidden = YES;
    self.labAddress.hidden = YES;
    self.labTime.hidden = YES;
    self.labEndTime.hidden = YES;
    self.labContent.hidden = YES;
    
    [self.textViewContent.layer setMasksToBounds:YES];
    [self.textViewContent.layer setBorderWidth:1];
    [self.textViewContent.layer setCornerRadius:8];
    [self.textViewContent.layer setBorderColor:RGBColor(230, 230, 230).CGColor];
    
    [self.butSend.layer setMasksToBounds:YES];
    [self.butSend.layer setCornerRadius:self.butSend.frame.size.height/2];
    self.butSend.backgroundColor = themeColor;
    [self.butSend setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.textViewContent.delegate = self;
    [self.view tapGesture:^(UIGestureRecognizer *ges) {
        [self clickSingleDynamic];
    }];
    NSString * str = [[NSString alloc]init];
    if([self.industryMg.feedback isEqualToString:@"false"]){
        str = @"确认";
    }else{
        str = @"已确认";
    }
    self.rightBarItem = [[UIBarButtonItem alloc]initWithTitle:str style:UIBarButtonItemStylePlain target:self action:@selector(clickButOk)];
    if([self.industryMg.feedback isEqualToString:@"false"]){
        [self.rightBarItem setTintColor:[UIColor redColor]];
    }else{
        [self.rightBarItem setTintColor:[UIColor whiteColor]];
    }
    self.navigationItem.rightBarButtonItem = self.rightBarItem;
    
    
    [self setUI];
    
    
    //增加监听，当键盘出现或改变时收出消息
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShowDetails:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    //增加监听，当键退出时收出消息
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHideDetails:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
}
-(void)setUI
{
    self.tableViewDetails.backgroundColor = [UIColor whiteColor];

    [self.tableViewDetails registerNib:[UINib nibWithNibName:@"LSZTongZhiDetailsTableViewCell" bundle:nil] forCellReuseIdentifier:@"LSZTongZhiDetailsTableViewCell"];
}
- (IBAction)butFanKui:(id)sender {
    FeedbackInformationList * list = [[FeedbackInformationList alloc]init];
    [list show];
    [list initWithAudioPlaylist:self.industryMg.feedbackContent];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 6;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row == 5){
        CGFloat height = [LSZSpecialPackaging getHeightOfText:ScreenWidth-20 height:ScreenHeight*2 font:14 :[NSString stringWithFormat:@"任务内容:%@",self.industryMg.content]];
        
        return height;
    }
    return 40;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        }
        cell.textLabel.text = self.industryMg.subject;
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        cell.textLabel.font = FONT_(15);
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else{
        LSZTongZhiDetailsTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LSZTongZhiDetailsTableViewCell" forIndexPath:indexPath];
        switch (indexPath.row) {
            case 1:
                {
                    cell.oneLabel.text = [NSString stringWithFormat:@"任务主题:%@",self.industryMg.subject];
                }
                break;
            case 2:
            {
                cell.oneLabel.text = [NSString stringWithFormat:@"任务地点:%@",self.industryMg.address];
            }
                break;
            case 3:
            {
                NSArray *array = [self.industryMg.activityTime componentsSeparatedByString:@" "]; //从字符A中分隔成2个元素的数组
                cell.oneLabel.text = [NSString stringWithFormat:@"任务开始时间:%@",[NSString stringWithFormat:@"%@",array[0]]];
            }
                break;
            case 4:
            {
                NSArray *array = [self.industryMg.publishTime componentsSeparatedByString:@" "]; //从字符A中分隔成2个元素的数组
                cell.oneLabel.text = [NSString stringWithFormat:@"任务结束时间:%@",[NSString stringWithFormat:@"%@",array[0]]];
            }
                break;
            case 5:
            {
                cell.oneLabel.text = [NSString stringWithFormat:@"任务内容:%@",self.industryMg.content];
                CGFloat height = [LSZSpecialPackaging getHeightOfText:ScreenWidth-20 height:ScreenHeight*2 font:14 :[NSString stringWithFormat:@"任务内容:%@",self.industryMg.content]];
                cell.oneLabl_height.constant = height;
            }
                break;
            default:
                break;
        }
        
        return cell;
    }
}
-(UITableView *)tableViewDetails
{
    if (_tableViewDetails == nil) {
        _tableViewDetails = [[UITableView alloc]initWithFrame:CGRectMake(0, NAVIGATION_BAR_HEIGHT,ScreenWidth ,ScreenHeight-60-NAVIGATION_BAR_HEIGHT) style:UITableViewStyleGrouped];
        _tableViewDetails.delegate = self;
        _tableViewDetails.dataSource = self;
        _tableViewDetails.estimatedRowHeight=0;
        _tableViewDetails.estimatedSectionFooterHeight =0;
        _tableViewDetails.estimatedSectionHeaderHeight =0;
        _tableViewDetails.separatorStyle = UITableViewCellEditingStyleNone;     //让tableview不显示分割线
//        [_tableViewDetails setSeparatorInset:UIEdgeInsetsMake(0, 20, 0, 20)];
        [self.view addSubview:_tableViewDetails
         ];
    }
    return _tableViewDetails;
}
//当键盘出现或改变时调用
- (void)keyboardWillShowDetails:(NSNotification *)aNotification
{
    //获取键盘的高度
    NSDictionary *userInfo = [aNotification userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    int height = keyboardRect.size.height;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3];
    self.lowView.frame = CGRectMake(0, ScreenHeight-self.lowView.frame.size.height-height, ScreenWidth, self.lowView.frame.size.height);

    CGRect rect = self.tableViewDetails.frame;
    rect.size.height = ScreenHeight-self.lowView.frame.size.height-height;
    self.tableViewDetails.frame = rect;
    
    [UIView commitAnimations];
}
//当键退出时调用
- (void)keyboardWillHideDetails:(NSNotification *)aNotification{
    [self keyboardWhereabouts];
}
-(void)keyboardWhereabouts
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3];
    self.lowView.frame = CGRectMake(0, ScreenHeight-self.lowView.frame.size.height, ScreenWidth, self.lowView.frame.size.height);
    CGRect rect = self.tableViewDetails.frame;
    rect.size.height = ScreenHeight-self.lowView.frame.size.height;
    self.tableViewDetails.frame = rect;
    [self clickSingleDynamic];
    [UIView commitAnimations];
}
-(void)clickSingleDynamic
{
    [self.view endEditing:YES];
}
#pragma mark - UITextViewDelegate
- (void)textViewDidEndEditing:(UITextView *)textView
{
    if(textView.text.length < 1){
        textView.text = @"请填写反馈内容";
        textView.textColor = [UIColor grayColor];
//        self.butSend.backgroundColor = RGBColor(212, 212, 212);
//        [self.butSend setTitleColor:RGBColor(178, 178, 178) forState:UIControlStateNormal];
    }else{
        textView.textColor=[UIColor blackColor];
//        self.butSend.backgroundColor = themeColor;
//        [self.butSend setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }
}
- (void)textViewDidBeginEditing:(UITextView *)textView
{
    if([textView.text isEqualToString:@"请填写反馈内容"]){
        textView.text=@"";
        textView.textColor = [UIColor grayColor];
//        self.butSend.backgroundColor = RGBColor(212, 212, 212);
//        [self.butSend setTitleColor:RGBColor(178, 178, 178) forState:UIControlStateNormal];
    }else{
        textView.textColor=[UIColor blackColor];
//        self.butSend.backgroundColor = themeColor;
//        [self.butSend setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
     }
}
//- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
//    NSString * str = [textView.text stringByReplacingCharactersInRange:range withString:text];
//    if (str.length >0) {
//        self.butSend.backgroundColor = themeColor;
//        [self.butSend setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    }else{
//        self.butSend.backgroundColor = RGBColor(212, 212, 212);
//        [self.butSend setTitleColor:RGBColor(178, 178, 178) forState:UIControlStateNormal];
//    }
//    return YES;
//}
#pragma mark - 点击发送按钮
- (IBAction)sendButClick:(id)sender {
    NSLog(@"点击发送按钮");
    if ([NullStringjudgment(NSStr(self.textViewContent.text)) isEqualToString:@""]) {
        [self initWithtring:@"请先输入反馈信息!"];
        return;
    }
    [self keyboardWhereabouts];
    [LSZRequestData feedbackInformation:self.industryMg.id_id  :self.textViewContent.text :^(BOOL isOK, NSString * string, NSDictionary * dic) {
        if (isOK == YES) {
            self.textViewContent.text = @"";
            [self initWithtring:@"反馈成功!"];
        }else{
            [self initWithtring:string];
        }
    }];
}
#pragma mark - 确认
-(void)clickButOk
{
    if(![self.industryMg.feedback isEqualToString:@"false"]){
        [self initWithtring:@"已经确认过了!"];
        return;
    }
    [LSZRequestData notificationConfirmation:self.industryMg.id_id :^(BOOL isoK, NSString * string, NSDictionary * dic) {
        if (isoK == YES) {
            [self initWithtring:string];
            [self.rightBarItem setTitle:@"已确认"];
            [self.rightBarItem setTintColor:[UIColor whiteColor]];
            self.industryMg.feedback = @"ture";
        }else{
            [self initWithtring:string];
        }
    }];
}
@end
